package com.eagle.view.widget;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.SurfaceHolder;

import com.eagle.support.utils.YuvRender;
import com.kinstalk.voip.jni.NVDataRender;
import com.kinstalk.voip.sdk.common.Log;

public class ConferenceRemoteVideoView extends GLSurfaceView {
    private YuvRender mRemoteRender;
    private NVDataRender mRemoteDataRender;

    public ConferenceRemoteVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mRemoteRender = new YuvRender();
        this.mRemoteRender.setDataRender(mRemoteDataRender);
        this.setEGLContextClientVersion(2);
        this.setRenderer(mRemoteRender);
        this.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        this.setKeepScreenOn(true);
    }

    public void process(long pointer, int width, int height, char rotation) {
        mRemoteDataRender.CopytoRenderBuffer(pointer, width, height);
        if (!mRemoteRender.IsSetuped())
            mRemoteRender.SetupRender(width, height);
        mRemoteRender.updateFrameInfo(width, height, rotation);
        this.requestRender();
    }

    public void doClose() {
        try {
            if (mRemoteDataRender != null) {
                mRemoteDataRender.DrawFinish();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("cim", "JOIN10 remote view error!" + e.getMessage());
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        super.surfaceCreated(holder);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        super.surfaceDestroyed(holder);
        doClose();
    }
}
